package ProjectCodeEngine;

import java.util.regex.Pattern;

import static ProjectCodeEngine.CodeType.AUTOMATIC_WELDING;
import static ProjectCodeEngine.CodeType.COMBINE_WELDING;
import static ProjectCodeEngine.CodeType.MANUAL_WELDING;
import static ProjectCodeEngine.CodeType.MECHANICAL_WELDING;

/**
 * 默认的技能评定判断器
 *
 * @author zzy
 * @version V1.0
 * @since 2022/8/4
 */
public class DefaultTypeJudgeRule implements TypeJudgeRule {

    // 自动焊正则表达式
    private static String pattenAutomatic = ".*\\-A";
    private static Pattern pa = Pattern.compile(pattenAutomatic);

    // 机械化焊正则表达式
    private static String pattenMachine = ".*\\-M";
    private static Pattern pm = Pattern.compile(pattenMachine);

    /**
     * 获取技能评定项目代号类型
     *
     * @return
     */
    public String getType(String projectCode) {

        // 切分项目代号
        SplitRule splitRule = new SplitBlankRule();
        // 项目代号拆分数组
        String[] parts = splitRule.split(projectCode);

        // 组合焊
        if (typeContains(parts)) {
            return COMBINE_WELDING;
        }

        // 机械化
        if (pm.matcher(parts[0]).find()) {
           return MECHANICAL_WELDING;
        }

        // 自动化焊
        else if (pa.matcher(parts[1]).find()) {
            return AUTOMATIC_WELDING;
        }

        // 手工焊
        else {
            return MANUAL_WELDING;
        }

    }

    /**
     * 判断是否是组合焊
     *
     * @param parts
     * @return
     */
    private boolean typeContains(String[] parts) {
        for (int i = 0; i < parts.length; i++) {
            if ("和".equals(parts[i])) {
                return true;
            }
        }
        return false;
    }


}
